View Javadoc
1 /* 2 * Scope: a generic MVC framework. 3 * Copyright (c) 2000-2002, The Scope team 4 * All rights reserved. 5 * 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * Neither the name "Scope" nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 * 36 * $Id: SModelButton.java,v 1.2 2002/09/13 17:03:50 ludovicc Exp $ 37 */ 38 package org.scopemvc.view.swing; 39 40 import javax.swing.JToolTip; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 import org.scopemvc.core.Control; 44 import org.scopemvc.core.Controller; 45 import org.scopemvc.core.PropertyView; 46 import org.scopemvc.core.Selector; 47 import org.scopemvc.view.util.ModelBindable; 48 49 /*** 50 * <p> 51 * 52 * An SButton that is bound to a property and performs a test on the value of 53 * the property to determine its active state. </P> <P> 54 * 55 * A comparable object is used to perform the test on the view value. This 56 * button is active when the comparable object returns a value greater than 0 57 * when passed the view value in its {@link Comparable#compareTo compareTo()} 58 * method.</P> <P> 59 * 60 * Note: it is convenient to use the Comparable interface to perform tests 61 * because it is already implemented in many places (natural ordering). For 62 * example, to have this SModelButton enabled when the view value is an Integer 63 * less than 1, then do: <br> 64 * <code>setValueTest(new Integer(1))</code> <br> 65 * because <code>new Integer(1).compareTo(value)</code> will return 1 if value 66 * is an Integer less than 1</P> <P> 67 * 68 * If the comparison fails because of an exception coming from the Comparable 69 * test, then this button is disabled. </P> 70 * 71 * @author <a href="mailto:steve.jones@netdecisions.co.uk>;Steve Jones</a> 72 * @author <a href="mailto:ludovicc@users.sourceforge.net>;Ludovic Claude</a> 73 * @created 03 September 2002 74 * @version $Revision: 1.2 $ $Date: 2002/09/13 17:03:50 $ 75 */ 76 public class SModelButton extends SButton 77 implements ModelBindable, PropertyView, Refreshable { 78 79 private static final Log LOG = LogFactory.getLog(SModelButton.class); 80 81 /*** 82 * Helper to manage model to view binding. 83 */ 84 private SwingBoundModel boundModel = new SwingBoundModel(this); 85 86 // --------------------- shownModel ----------------------- 87 88 /*** 89 * The model object that this component presents, which may be a property of 90 * the bound model if a Selector is specified. 91 */ 92 private Object shownModel; 93 94 private Comparable valueTest; 95 96 /*** 97 * Constructor for the SModelButton object. <BR> 98 * It defines a test that will activate the button if the view value is not 99 * null. 100 */ 101 public SModelButton() { 102 super(); 103 setValueTest(new SModelAction.NotNullComparable()); 104 } 105 106 /*** 107 * Constructor for the SModelButton object. <BR> 108 * It defines a test that will activate the button if the view value is not 109 * null. 110 * 111 * @param inControlID The control ID to be issued by this Button 112 */ 113 public SModelButton(String inControlID) { 114 super(inControlID); 115 setValueTest(new SModelAction.NotNullComparable()); 116 } 117 118 /*** 119 * Constructor for the SModelButton object. <BR> 120 * It defines a test that will activate the button if the view value is not 121 * null. 122 * 123 * @param inControlID The control ID to be issued by this Button 124 * @param inSelector The selector for the property 125 */ 126 public SModelButton(String inControlID, Selector inSelector) { 127 this(inControlID, inSelector, new SModelAction.NotNullComparable()); 128 } 129 130 /*** 131 * Constructor for the SModelButton object. <BR> 132 * It defines a test that will activate the button if the view value is not 133 * null. 134 * 135 * @param inControlID The control ID to be issued by this Button 136 * @param inSelector The selector for the property 137 * @param inValueTest The test for the enabled state. If the compareTo() 138 * method returns a value greater than 0, then this button is active. 139 */ 140 public SModelButton(String inControlID, Selector inSelector, Comparable inValueTest) { 141 super(inControlID); 142 setSelector(inSelector); 143 setValueTest(inValueTest); 144 } 145 146 // -------------- implement View ------------------ 147 148 /*** 149 * Gets the bound model 150 * 151 * @return The boundModel value 152 */ 153 public final Object getBoundModel() { 154 return boundModel.getBoundModel(); 155 } 156 157 /*** 158 * Gets the selector 159 * 160 * @return The selector value 161 */ 162 public final Selector getSelector() { 163 return boundModel.getSelector(); 164 } 165 166 /*** 167 * Get the current value (what would be set as a property of the bound model 168 * object) being presented on the View. 169 * 170 * @return property's value from the UI. 171 */ 172 public final Object getViewValue() { 173 return shownModel; 174 } 175 176 /*** 177 * Returns the Comparable used to test the view value. 178 * 179 * @return The valueTest value 180 */ 181 public Comparable getValueTest() { 182 return valueTest; 183 } 184 185 /*** 186 * Sets the selector 187 * 188 * @param inSelector The new selector value 189 */ 190 public final void setSelector(Selector inSelector) { 191 boundModel.setSelector(inSelector); 192 } 193 194 /*** 195 * Sets the selector string 196 * 197 * @param inSelectorString The new selectorString value 198 */ 199 public final void setSelectorString(String inSelectorString) { 200 boundModel.setSelectorString(inSelectorString); 201 } 202 203 /*** 204 * Sets the Comparable used to test the view value. <BR> 205 * This action is enabled when the compareTo() method of the test returns a 206 * value greater than 0 when the passed value is the model value bound to 207 * this SModelButton. 208 * 209 * @param inValueTest The new valueTest value 210 */ 211 public void setValueTest(Comparable inValueTest) { 212 valueTest = inValueTest; 213 updateEnabledState(); 214 } 215 216 /*** 217 * Sets the bound model 218 * 219 * @param inModel The new boundModel value 220 */ 221 public void setBoundModel(Object inModel) { 222 boundModel.setBoundModel(inModel); 223 } 224 225 // --------------------- Implement ModelBindable ---------------------- 226 227 /*** 228 * Use the passed property value and read-only state to update the View. 229 * <BR> 230 * Ignores inReadOnly. 231 * 232 * @param inValue The new value of the property in the bound model 233 * @param inReadOnly The new read-only state of the property 234 */ 235 public void updateFromProperty(Object inValue, boolean inReadOnly) { 236 if (LOG.isDebugEnabled()) { 237 LOG.debug("updateFromProperty: " + inValue + ", " + inReadOnly); 238 } 239 240 setShownModel(inValue); 241 } 242 243 /*** 244 * Validation failed while getting a value from View into the bound model 245 * object. <BR> 246 * Does nothing here. 247 * 248 * @param inException The exception causing the validation failure 249 */ 250 public void validationFailed(Exception inException) { 251 // noop 252 } 253 254 /*** 255 * Clear previous validation failure. <BR> 256 * Does nothing here. 257 */ 258 public void validationSuccess() { 259 // noop 260 } 261 262 263 // ------------------ Refreshable ------------------------- 264 265 /*** 266 * Update the widget with the current state of the bound model. 267 */ 268 public void refresh() { 269 Object propertyValue = boundModel.getPropertyValue(); 270 boolean propertyReadOnly = boundModel.getPropertyReadOnly(); 271 updateFromProperty(propertyValue, propertyReadOnly); 272 } 273 274 275 /*** 276 * Now overwrite the firing of the control to include the additional 277 * information of the model 278 * 279 * @return The Control to fire 280 */ 281 protected Control createControl() { 282 Control returnValue = super.createControl(); 283 if (LOG.isDebugEnabled()) { 284 LOG.debug("createControl: Creating the control " + returnValue + "with value " + this.shownModel); 285 } 286 287 returnValue.setParameter(this.shownModel); 288 return returnValue; 289 } 290 291 /*** 292 * Called internally from updateFromProperty(). Issues a 293 * CHANGE_MODEL_CONTROL_ID Control to notify parent Controller of the 294 * change. 295 * 296 * @param inModel The new shownModel value 297 */ 298 private void setShownModel(Object inModel) { 299 300 if (shownModel == inModel) { 301 return; 302 } 303 shownModel = inModel; 304 updateEnabledState(); 305 } 306 307 private void updateEnabledState() { 308 try { 309 if (LOG.isDebugEnabled()) { 310 LOG.debug("updateEnabledState: Testing " + shownModel + " with test " + valueTest + ", result: " + valueTest.compareTo(shownModel)); 311 } 312 this.setEnabled(valueTest.compareTo(shownModel) > 0); 313 } catch (NullPointerException ex) { 314 LOG.info("NPE when testing the view value with the valueTest. Assuming that this action: " + this.getName() + " is disabled"); 315 setEnabled(false); 316 } catch (Exception ex) { 317 LOG.warn("Could not test the view value with the valueTest", ex); 318 setEnabled(false); 319 } 320 } 321 322 } 323

This page was automatically generated by Maven